<template>
  <div>
    <div class="head">
      <el-button type="primary" size="small" @click="visiable=true">添加</el-button>
    </div>
    <div>
      <el-table :data="tableData">
        <el-table-column label="ID" width="50px" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column label="词汇" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.word}}</span>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.orderBy}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.createTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible.sync="visiable" width="450px">
      <el-form :model="form">
        <el-form-item label="词汇">
          <el-input placeholder="请输入" v-model="form.word" style="width:350px" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input placeholder="值越小越靠前" v-model="form.orderBy" style="width:350px" />
        </el-form-item>
      </el-form>
      <div class="bottom">
        <el-button type="primary" size="small" @click="add">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, list, del } from "../../../api/word.js";
export default {
  data() {
    return {
      tableData: [],
      title: "添加",
      visiable: false,
      form: {
        id: null,
        word: "",
        orderBy: 0
      }
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    del(id) {
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del(id).then(res => {
            this.$message({
              type: "success",
              message: res.message
            });
            this.initData()
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    edit(data) {
      this.visiable = true;
      this.form.id = data.id;
      this.form.word = data.word;
      this.form.orderBy = data.orderBy;
    },
    add() {
      add(this.form.id, this.form.word, this.form.orderBy).then(res => {
        this.$message({
          type: "success",
          message: res.message
        });
        this.visiable = false;
        this.form = {
          id: null,
          word: "",
          orderBy: 0
        };
        this.initData();
      });
    },
    initData() {
      list().then(res => {
        this.tableData = res.data;
      });
    }
  }
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
.bottom {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  padding-right: 30px;
}
</style>
